<?php

class Model_Event extends Model {

    function __construct() {

    }

    /* 獲得Event列表資料
     * $orderby	string	 	字段名 desc 或 字段名 asc
     * $type		int		活動分類
     * $state	int		0全部	1邀請時間範圍內	2已錯過邀請時間 3有活動報告內容
     * $page		int			當前頁數
     * $size		int			獲取個數
     * return	array		資料數組
     */

    function getEventList($type = 0, $state = 0, $orderby = "TB1.id desc", $page = 1, $size = 3, $keyword = '') {

        $where = "TB1.status=1";
        if (intval($type))
            $where .=" and TB1.event_type=" . intval($type);

        switch ($state) {
            case 1:
                //$where .= " and '".date('Y-m-d H:i:s', time())."' between TB1.starttime and TB1.endtime ";
                $where .= " and '" . date('Y-m-d H:i:s', time()) . "' < TB1.endtime ";
                break;
            case 2:
                $where .= " and '" . date('Y-m-d H:i:s', time()) . "' >= TB1.endtime  and has_detail=0";
                break;
            case 3:
                $where .= " and has_detail=1";
                break;
            case 0:
                $where .= "";
                break;

            default:
                return false;
                break;
        }

        if ($keyword)
            $where .=" and (TB1.title like '%" . $keyword . "%' or TB1.brand_str like '%" . $keyword . "%')";

        $param = array();

        $param['field'] = "TB1.id,TB1.points,TB1.event_url,TB1.cover_image1 as cover,TB1.cover2_image1 as cover2,TB1.member_type,TB1.title,TB1.starttime,TB1.endtime,TB1.event_type,TB1.event_date,TB1.event_time,TB1.place,TB1.intro,TB1.content_json,TB1.questions_json,TB1.product,TB1.brand_str,TB1.click,TB1.likes,TB1.quota,TB1.apply_num,TB1.quota,TB1.has_detail,TB1.body,TB1.terms,TB1.video,TB1.video_details,TB2.name,TB2.cname ";
        $param['where'] = $where;
        $param['order'] = $orderby; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = $size;
        $param['page'] = $page;
        $param['join'] = "LEFT JOIN event_type";
        $param['on'] = "TB1.event_type = TB2.id";
        $data = db_query_hash('event', $param);

        return $data;
    }

    /* 獲得Event詳細資料
     * $id int Event ID
     * return	array		資料數組
     */

    function getEventById($id = 0,$status = 1) {
        if (!intval($id))
            return false;
        $result = array();

        $where = "TB1.id =" . intval($id);
        
        if (intval($status) == 1) {
            $where .=" and TB1.status=1";
        }
        
        $param['field'] = "TB1.id,TB1.points,TB1.event_url,TB1.cover_image1 as cover,TB1.cover2_image1 as cover2,TB1.member_type,TB1.title,TB1.starttime,TB1.endtime,TB1.event_type,TB1.event_date,TB1.event_time,TB1.place,TB1.intro,TB1.content_json,TB1.questions_json,TB1.product,TB1.brand_str,TB1.click,TB1.likes,TB1.quota,TB1.apply_num,TB1.quota,TB1.has_detail,TB1.body,TB1.terms,TB1.video,TB1.video_details,TB2.name,TB2.cname,TB1.status ";
        $param['where'] = $where;
        $param['order'] = ''; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = '';
        $param['page'] = '';
        $param['join'] = "LEFT JOIN event_type";
        $param['on'] = "TB1.event_type = TB2.id";
        $data = db_query_hash('event', $param);

        if ($data['result']) {
            $result = $data['result'][0];

            $param = array();
            $param['field'] = "";
            $param['where'] = "oid=" . $result['id'];
            $param['order'] = ''; //"id asc"
            $param['group'] = "";
            $param['limit'] = "";
            $param['pagesize'] = '';
            $param['page'] = '';
            $param['join'] = "";
            $param['on'] = "";
            $data_event_user = db_query_hash('event_user', $param);

            if ($data_event_user['result']) {


                foreach ($data_event_user['result'] as $key => $value) {
                    $param = array();
                    $param['field'] = "";
                    $param['where'] = "oid=" . $value['id'];
                    $param['order'] = ''; //"id asc"
                    $param['group'] = "";
                    $param['limit'] = "";
                    $param['pagesize'] = '';
                    $param['page'] = '';
                    $param['join'] = "";
                    $param['on'] = "";
                    $data_event_user_blog = db_query_hash('event_user_blog', $param);

                    if ($data_event_user_blog['result']) {
                        $data_event_user['result'][$key]['event_user_blog'] = $data_event_user_blog['result'];
                    }
                }

                $result['event_user'] = $data_event_user['result'];
            }
        }

        return $result;
    }

    function get_bt($i, $v) {
        $bt_arr = array(
            0 => array(
                '_statu' => 0,
                '_button_css' => "btn_begin",
                '_button_txt' => "即將開始",
                '_button_url' => optimize_url('events', 'invite_inpage', 'eid=' . $v['id'])
            ),
            1 => array(
                '_statu' => 1,
                '_button_css' => "btn_join",
                '_button_txt' => "立即參加",
                '_button_url' => optimize_url('events', 'invite_inpage', 'eid=' . $v['id'])
            ),
            2 => array(
                '_statu' => 2,
                '_button_css' => "btn_result",
                '_button_txt' => "查看申請結果",
                '_button_url' => optimize_url('events', 'invite_result', 'eid=' . $v['id'])
            ),
            3 => array(
                '_statu' => 3,
                '_button_css' => "btn_l",
                '_button_txt' => "觀看活動報導",
                '_button_url' => optimize_url('events', 'detail', 'eid=' . $v['id'])
            ),
            4 => array(
                '_statu' => 4,
                '_button_css' => "btn_join",
                '_button_txt' => "立即參加",
                '_button_url' => $v['event_url']
            )
        );
        return $bt_arr[$i];
    }

    function update_i_agree($event, $mid) {
        $product_str = $event['product'];
        $products = !empty($product_str) ? db_query_hash('product', array('field' => 'id,report_endtime', 'where' => 'id in(' . $product_str . ')')) : array();
        $member_applications = !empty($product_str) ? db_query_hash('member_application', array('field' => 'product', 'where' => 'mid=' . $mid . ' and product in(' . $product_str . ')')) : array();

        $temp = array();
        if (!empty($member_applications['result'])) {
            foreach ($member_applications['result'] as $k => $v) {
                $temp[] = $v['product'];
                //db_update('product_application',array('application_status'=>9),'mid='.$this->member['id'].' and oid ='.$v['product']);
            }
        }
        $res = 0;
        if (!empty($products['result'])) {
            foreach ($products['result'] as $k => $v) {
                if (empty($temp) || !in_array($v['id'], $temp)) {
                    echo $v['id'];
                    $attr = array();
                    $attr['mid'] = $mid;
                    $attr['product'] = $v['id'];
                    $attr['delivery_status'] = 1;
                    $attr['report_endtime'] = $event['endtime'];
                    $attr['delivery_status'] = 1;
                    $attr['status'] = 1;
                    $res = db_add('member_application', $attr);
                }
            }
        }
        return $res;
    }

    function get_list_res($data_res,$s) {
        $res = isset($data_res) && !empty($data_res) ? $data_res : array();
        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $res[$k]['_difTime'] = explode(" ", str_diff_time($v['endtime']));
                $res[$k]['_photo_url'] = !empty($v['has_detail']) ? PhotoResize($v['cover2'], 158, 158) : PhotoResize($v['cover'], 158, 158);
                $res[$k]['intro'] = wordscut($v['intro'], 50);
                //event button url and txt
                $_temp = array();
                if (!empty($v['event_url']) && intval($v['has_detail']) > 0) {
                    $_temp = $this->get_bt(4, $v);
                } elseif ($s == 0) {
                    $_temp = $this->get_bt(1, $v);
                    if (empty($res[$k]['_difTime'][0])) {
                        $_temp = $this->get_bt(2, $v);
                    }
                    if (intval($v['has_detail']) > 0 && $s <> 2) {
                        $_temp = $this->get_bt(3, $v);
                    }
                    if (time() < strtotime($v['starttime'])) {
                        $_temp = $this->get_bt(0, $v);
                    }
                } elseif ($s == 1) {
                    if (time() < strtotime($v['starttime'])) {
                        $_temp = $this->get_bt(0, $v);
                    } else {
                        $_temp = $this->get_bt(1, $v);
                    }
                } elseif ($s == 2) {
                    $_temp = $this->get_bt(2, $v);
                } elseif ($s == 3) {
                    $_temp = $this->get_bt(3, $v);
                }
                $res[$k] = array_merge($res[$k], $_temp);
            }
        }
        return $res;
    }

}

?>